package torres;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import torres.ability.AbilityManager;
import torres.entity.Avatar;
import torres.entity.Merchant;
import torres.entity.Occupation.Job;
import torres.entity.Smasher;
import torres.entity.Sneak;
import torres.entity.Summoner;
import torres.fileIO.Saver;
import torres.item.ItemGenerator;
import torres.item.ItemManager;
import torres.item.TakeableItem;
import torres.map.Location;
import torres.map.Map;
import torres.map.MapGenerator;
import torres.map.World;
import torres.view.MainView;

public class RunGame {
	
	public static GameLoop gameLoop;
	public static Thread t;
	
	public static void main(String args[]) throws Exception{
		
		World world = World.getInstance();
		boolean load = false;
		//Saver.load(new File("test.txt"));
		
		/*File mapFile1 = new File("src/resources/maps/map1.map");
		File enemyFile1 = new File("src/resources/maps/map1.enm");
		File mapFile2 = new File("src/resources/maps/map2.map");
		File mapFile3 = new File("src/resources/maps/map3.map");
		File mapFile4 = new File("src/resources/maps/map4.map");
		File mapItemFile1 = new File("src/resources/maps/map1.itm");
		File itemFile = new File("src/resources/items/items.itm");
		File abilityFile = new File("src/resources/abilities/abilities.abl");
		File levelRequirementFile = new File("src/resources/abilities/levelRequirement.abl");
		
		ItemManager itemManager = ItemManager.getInstance();
		AbilityManager abilityManager = AbilityManager.getInstance();
		
		itemManager.generate(itemFile);
		abilityManager.generate(abilityFile);
		abilityManager.generateLevelRequirementList(levelRequirementFile);
		
		Map map1 = new Map(mapFile1, "map1");
		Map map2 = new Map(mapFile2, "map2");
		Map map3 = new Map(mapFile3, "map3");
		Map map4 = new Map(mapFile4, "map4");
		
		world.addMap(map1);
		world.addMap(map2);
		world.addMap(map3);
		world.addMap(map4);
		world.setCurrentMap(map1);
		world.setCombatArena(map3);
		
		ItemGenerator.generate(mapItemFile1);
		MapGenerator.generateEnemies(map1, enemyFile1);
				
		Avatar player = new Avatar("Clopez" ,new Smasher());
		player.setLocation(new Location(3, 3));
		world.setPlayer(player);
		world.addEntity(player, player.getLocation());*/
		
		/*
		 * Location loc = new Location(5, 5);
		Location loc1 = new Location(5, 6);
		Location loc2 = new Location(5, 7);
		Location loc3 = new Location(5, 8);

		System.out.println(World.getInstance().hasEntity(loc));
		System.out.println(World.getInstance().hasEntity(loc1));
		System.out.println(World.getInstance().hasEntity(loc2));
		System.out.println(World.getInstance().hasEntity(loc3));
		*/
		
		/*Merchant merchant = new Merchant(new ArrayList<TakeableItem>());
		merchant.setLocation(new Location(0,0 ));
		world.addEntity(merchant, merchant.getLocation());*/
		
		/*
		AI ai = new PassiveAI();
		Collection<AI> aiCollection = new ArrayList<AI>();
		ArrayList<Entity> enemyList = world.getEnemyList();
		for (int i = 0; i < 5; i++) {
			Entity npc = new NPC();
			npc.setLocation(new Location(4+i, 4));
			enemyList.add(npc);
			ai.addEntity(npc);
			
			world.addEntity(npc, npc.getLocation());
		}
		aiCollection.add(ai);
		
		gameLoop = new GameLoop(aiCollection);
		*/
		/*gameLoop = new GameLoop();
		t = new Thread(gameLoop);
		t.setName("GameLoop" + t.getId());
		t.start();*/
		
		while(true) {
			
			//load = false;
			
			try{
			    
				String[] buttons = { "New Game", "Load Game"};

			    int choice = -1;
			    while(choice == -1) {
				    
			    	choice = JOptionPane.showOptionDialog(null, "Main Menu", "Main Menu",
				        JOptionPane.INFORMATION_MESSAGE, 1, null, buttons, buttons[0]);
			    	
				    if(choice == 1) {
				    		
				    		load = true;
				    }
				    else if(choice != 0)
				    	System.exit(1);
			    }
				if(choice == 1) {
					
					load = true;
				}
				else {
					
					//allow the player to choose a name
					String name = null;
					while(name==null) {
						
						name = JOptionPane.showInputDialog(null, "Name: ");
						if(name == null)
							System.exit(1);
						
					}
					
					//allow the player to choose an occupation and create the avatar
					Job occupation = null;
					while(occupation==null) {
						Job[] selectionValues = {Job.SMASHER, 
													Job.SUMMONER,
													Job.SNEAK};
					    Job initialSelection = Job.SMASHER;
					    occupation = (Job) JOptionPane.showInputDialog(null, "Occupation?",
					        "Occupation", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
					    
					    
					    if(occupation == null)
					    	System.exit(1);
					}
					
					Avatar player = null;
				    switch(occupation) {
				    	
				    	case SMASHER:
				    		player = new Avatar(name, new Smasher());
							break;
						case SUMMONER:
							player = new Avatar(name, new Summoner());
				    		break;
				    	case SNEAK:
				    		player = new Avatar(name, new Sneak());
				    		break;
				    }
				    
				    File mapFile1 = new File("src/resources/maps/map1.map");
					File enemyFile1 = new File("src/resources/maps/map1.enm");
					File mapFile2 = new File("src/resources/maps/map2.map");
					File mapFile3 = new File("src/resources/maps/map3.map");
					File mapFile4 = new File("src/resources/maps/map4.map");
					File mapItemFile1 = new File("src/resources/maps/map1.itm");
					File itemFile = new File("src/resources/items/items.itm");
					File abilityFile = new File("src/resources/abilities/abilities.abl");
					File levelRequirementFile = new File("src/resources/abilities/levelRequirement.abl");
					
					ItemManager itemManager = ItemManager.getInstance();
					AbilityManager abilityManager = AbilityManager.getInstance();
					
					itemManager.generate(itemFile);
					abilityManager.generate(abilityFile);
					abilityManager.generateLevelRequirementList(levelRequirementFile);
					
					//add maps to the world
					Map map1 = new Map(mapFile1, "map1");
					Map map2 = new Map(mapFile2, "map2");
					Map map3 = new Map(mapFile3, "map3");
					Map map4 = new Map(mapFile4, "map4");
					
					world.addMap(map1);
					world.addMap(map2);
					world.addMap(map3);
					world.addMap(map4);
					world.setCurrentMap(map1);
					world.setCombatArena(map3);
					
					ItemGenerator.generate(mapItemFile1);
					MapGenerator.generateEnemies(map1, enemyFile1);
					
					player.setLocation(new Location(3, 3));
					world.setPlayer(player);
					world.addEntity(player, player.getLocation());
					
					Merchant merchant = new Merchant(new ArrayList<TakeableItem>());
					merchant.setLocation(new Location(0,0 ));
					world.addEntity(merchant, merchant.getLocation());
					if(load)
						Saver.load(new File("test.txt"));
				}
				
				gameLoop = new GameLoop();
				t = new Thread(gameLoop);
				t.setName("GameLoop" + t.getId());
				t.start();
				
				t.join();
				
				//display restart prompt when the player dies
				int option = JOptionPane.showConfirmDialog(null, "Restart?", "You just died!", JOptionPane.YES_NO_OPTION);
				if(Math.abs(option)==1) {
					MainView.getInstance().dispose();
					System.exit(0);
					break;
				}
			}
			catch(Exception e) {
				
				e.printStackTrace();
			}
		}
	}
}
